import InfoBarList from '../InfoBarList/InfoBarList';

const backgroundId = 'e9424af07d8242adb4fa49abe40544b4';
const topId = 'df1df249e09c4fc19f63fc98378b9e98';

export default class RhHandler {
    constructor() {
        this.chart = null;
        this.infoObj = null;
        this.visible = false;
        this.num = 0;
        this.legendObj = {
            name: '能见度(km)',
            arrName: ['>20', '20-10', '10-4', '<4'],
            // arrColor: ['#28AD86', '#E0B62C', '#FE4B3C', '#8E71FF']
            arrColor: ['rgba(221, 221, 221, 0.05)', 'rgba(221, 221, 221, 0.3)', 'rgba(221, 221, 221, 0.5)', 'rgba(221, 221, 221, 0.7)']
        };
    }

    loadData(map, isInitChart) {
        let that = this;
        minemap.util.getJSON('//dev.minedata.cn/dataweather/weathertop10/wins', function (error, data) {
            const dataArr = data ? data.data.rows[0].features || [] : [];
            that.infoObj = {
                "color1": "#00C0CE",
                "color2": "#FEC819",
                "unit": "风力(级)",
                "array": [],
                callback: (coordinate) => {
                    map.flyTo({
                        center: coordinate,
                        zoom: 15
                    });
                }
            };
            dataArr.map((item) => {
                that.infoObj.array.push({
                    "name": item.properties.name_zh,
                    "value": item.properties.wins,
                    "coordinate": item.geometry.coordinates[Math.ceil(item.geometry.coordinates.length / 2)]
                })
            });
            if (isInitChart) {
                that.updateChart();
            }
        });
    }

    updateChart() {
        this.chart = new InfoBarList('road-weather-chart', this.infoObj || {});
    }

    getInfoObj() {
        return this.infoObj;
    }

    getChart() {
        return this.chart;
    }

    getLegendObj() {
        return this.legendObj;
    }

    setValueNum(map, valueNum) {
        if (valueNum === 24) valueNum = 0;
        if (this.visible) {
            map.moveLayer('vism' + this.num, backgroundId);
            this.num = valueNum;
            map.moveLayer('vism' + valueNum, topId);
        } else {
            this.num = valueNum;
        }
    }

    setVisible(map) {
        if (!this.visible) {
            map.moveLayer('vism' + this.num, topId);
            this.visible = true;
        }
    }

    setUnvisible(map) {
        if (this.visible) {
            map.moveLayer('vism' + this.num, backgroundId);
            this.visible = false;
        }
    }
};